package com.coldchain.cys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coldchain.common.msg.BaseResponse;
import com.coldchain.common.msg.ObjectRestResponse;
import com.coldchain.cys.entity.CarBrand;
import com.coldchain.cys.mapper.CarBrandMapper;
import com.coldchain.cys.page.CarBrandQueryVo;
import com.coldchain.cys.service.ICarBrandService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tianpeng
 * @since 2018-08-28
 */
@Service
public class CarBrandServiceImpl extends ServiceImpl<CarBrandMapper, CarBrand> implements ICarBrandService {
	
	@Override
	public void getList(CarBrandQueryVo vo) {
		QueryWrapper<CarBrand> ew = new QueryWrapper<CarBrand>();
		if(StrUtil.isNotBlank(vo.getName())) {
			ew.like("name", vo.getName()) ;
		}
		super.page(vo, ew) ;
	}

	@Transactional
	@Override
	public BaseResponse doAddCarBrand(CarBrand carBrand) {
		if(StrUtil.isBlank(carBrand.getName())) {
			return getErrorMessage("车辆型号名称不能为空") ;
		}
		boolean result = super.save(carBrand);
		if(!result) {
			return getErrorMessage("操作失败") ;
		}
		return new ObjectRestResponse<>().succeed("添加成功") ;
	}

	private BaseResponse getErrorMessage(String message) {
		return new ObjectRestResponse<>().error(message) ;
	}
}
